﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Data.Linq;
using CheckLift.Common.Intf;

namespace CheckLift.Linq
{
    public class CustomerData : BaseData
    {
        public CustomerData()
        {
            DataLoadOptions options = new DataLoadOptions();

            options.LoadWith<Customer>(c => c.Address);
            dc.LoadOptions = options;
        }

        public ICustomer GetCustomer(Guid customerId)
        {
            return dc.Customers.FirstOrDefault<ICustomer>(c => c.Id == customerId && c.IsActive == true);
            //return (from customer in dc.Customers
            //        join company in dc.Companies on customer.CompanyId equals company.Id
            //        join user_company_link in dc.User_Company_Links on company.Id equals user_company_link.CompanyId
            //        where customer.Id == customerId && customer.IsActive == true
            //        select customer
            //        ).FirstOrDefault<Customer>();
        }

        public ObservableCollection<Customer> GetAllCustomers( Guid userId)
        {
            return Convert<Customer>(from customer in dc.Customers
                                      join user_company_link in dc.User_Company_Links on customer.CompanyId equals user_company_link.CompanyId
                                      where user_company_link.UserId == userId && customer.IsActive == true
                                     select customer);
        }
    }
}
